package core.controller;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class Event {

    protected Map<String, String[]> parameters = new HashMap<String, String[]>();

    public Event(Map<String, String[]> parameters) {
        this.parameters = parameters;
    }

    /**
     * Retrieves a String parameter from the parameters list
     * 
     * @param parameter parameter name
     * @return value from the client site
     */
    public String getString(String parameter) {
        String[] values = getStringArray(parameter);
        if (values == null) {
            return null;
        }
        return values[0];
    }

    /**
     * Retrieves a int parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public int getInt(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null || values[0] == null || values[0].trim().isEmpty()) {
            return 0;
        }
        return new Integer(values[0]);
    }

    /**
     * Retrieves a float parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public float getFloat(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null) {
            return 0;
        }
        return new Float(values[0]);
    }

    /**
     * Retrieves a float array parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public float[] getFloatArray(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null) {
            return null;
        }
        float[] floatValues = new float[values.length];
        for (int i = 0; i < values.length; i++) {
            floatValues[i] = new Float(values[i]);
        }
        return floatValues;
    }

    /**
     * Retrieves a float array parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public Float[] getFloatWArray(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null) {
            return null;
        }
        Float[] floatValues = new Float[values.length];
        for (int i = 0; i < values.length; i++) {
            floatValues[i] = new Float(values[i]);
        }
        return floatValues;
    }

    /**
     * Retrieves a double parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public double getDouble(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null) {
            return 0;
        }
        return new Double(values[0]);

    }

    /**
     * Retrieves a double array parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public double[] getDoubleArray(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null) {
            return null;
        }
        double[] doubleValues = new double[values.length];
        for (int i = 0; i < values.length; i++) {
            doubleValues[i] = new Double(values[i]);
        }
        return doubleValues;
    }

    /**
     * Retrieves a double array parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public Double[] getDoubleWArray(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null) {
            return null;
        }
        Double[] doubleValues = new Double[values.length];
        for (int i = 0; i < values.length; i++) {
            doubleValues[i] = new Double(values[i]);
        }
        return doubleValues;
    }

    /**
     * Retrieves a boolean parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public boolean getBoolean(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null) {
            return false;
        }
        return Boolean.valueOf(values[0]);
    }

    /**
     * Retrieves a int array parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public int[] getIntArray(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null) {
            return null;
        }
        int[] intValues = new int[values.length];
        for (int i = 0; i < values.length; i++) {
            intValues[i] = new Integer(values[i]);
        }
        return intValues;
    }

    /**
     * Retrieves a Integer array parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public Integer[] getIntegerArray(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null) {
            return null;
        }
        Integer[] intValues = new Integer[values.length];
        for (int i = 0; i < values.length; i++) {
            intValues[i] = new Integer(values[i]);
        }
        return intValues;
    }

    /**
     * Retrieves a String[] parameter from the parameters list
     * 
     * @param parameter parameter name
     * @return value from the client site
     */
    public String[] getStringArray(String parameter) {
        return parameters.get(parameter);
    }

    /**
     * Retrieves a boolean array parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public boolean[] getBooleanArray(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null) {
            return null;
        }
        boolean[] intValues = new boolean[values.length];
        for (int i = 0; i < values.length; i++) {
            intValues[i] = new Boolean(values[i]);
        }
        return intValues;
    }

    /**
     * Retrieves a boolean array parameter from the parameters list
     * 
     * @param formFieldName parameter name
     * @return value from the client site
     */
    public Boolean[] getBooleanWArray(String formFieldName) {
        String[] values = getStringArray(formFieldName);
        if (values == null) {
            return null;
        }
        Boolean[] intValues = new Boolean[values.length];
        for (int i = 0; i < values.length; i++) {
            intValues[i] = new Boolean(values[i]);
        }
        return intValues;
    }
}
